<?php
require("class.phpmailer.php");
include("class.pop3.php");
include("class.smtp.php");
include_once 'koneksi.php';
class Password 
{
	public function __construct (){
		$this-> koneksi = new Koneksi();
	} //<!--end of __construct-->
	
	public function resetpassword ($id_user,$emailuser) {
		$this->koneksi->_connect();
		$newpass = $this->newpassword();
		//var_dump($id_user,$emailuser);
		$query = "SELECT id_user as id_user, emailuser as emailuser
                    FROM user
                    WHERE id_user = '$id_user' and emailuser = '$emailuser'";
		$result = mysql_query($query, $this-> koneksi -> db);
		$user = mysql_fetch_assoc($result);
		$this->koneksi->_close();
		$pesan ="";
		if(($user["id_user"]=="") or ($user["emailuser"]=="")){
			$pesan = "harap isi kolom username dan email dengan benar";
		} else if (($user["id_user"]==$id_user) and ($user["emailuser"]==$emailuser)){
			$this->koneksi->_connect();
			$query = mysql_query("UPDATE user SET password = md5('$newpass') WHERE id_user =
			 '$id_user' and emailuser = '$emailuser';");
			$this->koneksi->_close();
			$this->sendMail($emailuser);
			$pesan  = "password anda telah berubah, dan telah dikirimkan ke alamat "
			.$emailuser." ".$newpass;
		} else {
			$pesan = "maaf ada kesalahan sistem, coba lagi";
		}
		return $pesan;
	} //<!--end of reset password-->
	
	private function sendMail($emailuser){
		$mail = new PHPMailer();
		$from = "andi.saputra.jakarta@gmail.com"; 
		$to = $emailuser; 
		$name = $to;
		$mail->From = $from;
		$mail->FromName = "andi.saputra.jakarta@gmail.com"; 
		$mail->AddAddress($to,$to);
		$mail->Subject = "Penggantian password untuk user ".$id_user." pada pronsweb";
		$mail->AddReplyTo($from,"andi.saputra.jakarta@gmail.com");
		$mail->WordWrap = 50;                                 
		$mail->IsHTML(true);                                  
		$mail->Body = "<p>Kepada Yth: ".$id_user."</p>
		<p>Salam hangat dari kami,</p>
		<p>diberitahukan kepada anda bahwa password anda telah dirubah menjadi : <b>".$newpass." <b/></p>
		<p>Terima kasih,<br />
		Pronsweb</p>";
		$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
		if($mail->Send()){
			$reset=true;
		}
	}
	
	private function newpassword () {
		$chars = "abcdefghijkmnopqrstuvwxyz023456789";
        srand((double)microtime()*1000000);
        $i = 0;
        $pass = '' ;
        while ($i <= 7) {
            $num = rand() % 33;
            $tmp = substr($chars, $num, 1);
            $pass = $pass . $tmp;
            $i++;
        } //<!--end of while-->
        return $pass;
	} //<!--end of newpassword-->
} //<!--end of class-->
